﻿using System;
using System.Collections;
using System.Xml;
using System.Web.Configuration;
using System.Configuration;
 
namespace HttpModuleAccessControl
{
    class Config
    {
        private static string _configfile = "rbac.config";
        private static string _ConfigFilePath = @System.Configuration.ConfigurationManager.AppSettings["rbac.config"] + @"\" + _configfile;

        // Future : to read web.config or other configuration file
        private static string _DBConnectionString = string.Empty ;

        public static string DBConnectionString
        {
            get
            {
                System.Xml.XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(@_ConfigFilePath);
                XmlNode node1, node2, node3, node4;
                XmlNode root = xmldoc.DocumentElement;
                string _datasource, _initialcatalog, _userid, _password;

                node1 = root.SelectSingleNode("/configuration/database/datasource");
                if (node1.InnerText.Trim().Length > 0)
                {
                    _datasource = node1.InnerText.Trim();
                }
                else
                {
                    throw new System.FormatException("The length of DataSource cannot be equal to or less than zero in " + _configfile + " file.");
                }

                node2 = root.SelectSingleNode("/configuration/database/initialcatalog");
                if (node2.InnerText.Trim().Length > 0)
                {
                    _initialcatalog = node2.InnerText.Trim();
                }
                else
                {
                    throw new System.FormatException("The length of Initial Catalog cannot be equal to or less than zero in " + _configfile+ " file.");
                }

                node3 = root.SelectSingleNode("/configuration/database/userid");
                if (node3.InnerText.Trim().Length > 0)
                {
                    _userid = node3.InnerText.Trim();
                }
                else
                {
                    throw new System.FormatException("The length of User ID cannot be equal to or less than zero in " + _configfile + " file.");
                }

                node4 = root.SelectSingleNode("/configuration/database/password");
                if (node4.InnerText.Trim().Length > 0)
                {
                    // future : decrypt the encrypted password

                    _password = node4.InnerText.Trim();
                }
                else
                {
                    throw new System.FormatException("The length of Password cannot be equal to or less than zero in " + _configfile + " file.");
                }

                // You can add your own element readers

                _DBConnectionString = "data source=" + _datasource + ";initial catalog=" + _initialcatalog + ";user id=" + _userid + ";password=" + _password +";Asynchronous Processing=true;MultipleActiveResultSets=true;";

                return _DBConnectionString;
            }
        }

        public static string DatabaseName()
        {
            System.Xml.XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(@_ConfigFilePath);
            XmlNode  node2 ;
            XmlNode root = xmldoc.DocumentElement;
            string  _initialcatalog= string.Empty ;
            node2 = root.SelectSingleNode("/configuration/database/initialcatalog");
            if (node2.InnerText.Trim().Length > 0)
            {
                _initialcatalog = node2.InnerText.Trim();
            }
            else
            {
                throw new System.FormatException("The length of Database name cannot be equal to or less than zero in " + _configfile + " file.");
            }

            return _initialcatalog;

        }

        public static string ServiceName()
        {
            System.Xml.XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(@_ConfigFilePath);
            XmlNode node2;
            XmlNode root = xmldoc.DocumentElement;
            string _sn = string.Empty;
            node2 = root.SelectSingleNode("/configuration/ServiceBroker/ServiceName");
            if (node2.InnerText.Trim().Length > 0)
            {
                _sn = node2.InnerText.Trim();
            }
            else
            {
                throw new System.FormatException("The length of ServiceName cannot be equal to or less than zero in " + _configfile + " file.");
            }

            return _sn;

        }

        public static string QueueName()
        {
            System.Xml.XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(@_ConfigFilePath);
            XmlNode node2;
            XmlNode root = xmldoc.DocumentElement;
            string _qn = string.Empty;
            node2 = root.SelectSingleNode("/configuration/ServiceBroker/QueueName");
            if (node2.InnerText.Trim().Length > 0)
            {
                _qn = node2.InnerText.Trim();
            }
            else
            {
                throw new System.FormatException("The length of QueueName cannot be equal to or less than zero in " + _configfile + " file.");
            }

            return _qn;

        }


        public static bool ifLog
        {
            get
            {
                System.Xml.XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(@_ConfigFilePath);
                XmlNode node1;
                XmlNode root = xmldoc.DocumentElement;
                bool _answer = false;

                node1 = root.SelectSingleNode("/configuration/ifDebug");
                if (node1.InnerText.Trim().Length > 0)
                {
                    _answer = Convert.ToBoolean(node1.InnerText.Trim());
                }

                return _answer;
            }
        }
        // Future Change :: debug enable option


    }
}
